package com.example.stock.controller;

import com.example.stock.business.monitor.MonitorFactory;
import com.example.stock.controller.dto.MonitorPriceDTO;
import com.example.stock.excel.Monitor;
import com.example.stock.model.MonthDetail;
import com.example.stock.service.MonitorService;
import com.example.stock.service.MonthDataService;
import com.example.stock.util.NumberUtil;
import com.example.stock.util.Result;
import com.example.stock.util.ResultGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@CrossOrigin(origins = "*")
@RequestMapping("monitor")
@RestController
public class MonitorController {

    @Autowired
    MonitorFactory monitorFactory;

    @Autowired
    MonitorService monitorService;

    @Autowired
    MonthDataService monthDataService;

    @GetMapping
    public Result index() {

        List<Monitor> ret = monitorService.getAllMonitors()
                .stream()
                .filter(monitorFactory::trigger)
                .toList();

        return ResultGenerator.genSuccessResult(ret);
    }

    @GetMapping("list")
    public Result list(@RequestParam(required = false) String stock) {
        List<Monitor> list = monitorService.getAllMonitors();

        List<Monitor> ret;
        if (stock != null) {
            ret = list.stream().filter(i -> i.getStock().equals(stock)).toList();
        } else {
            ret = list.stream().sorted(Comparator.comparing(Monitor::getDate).reversed()).limit(250).toList();
        }

        return ResultGenerator.genSuccessResult(ret);
    }

    /**
     * 价格监控
     * @return
     */
    @GetMapping("price/{stock}")
    public Result priceStock(@PathVariable String stock) {
        List<Monitor> list = monitorService.getAllMonitors().stream()
                .filter(i -> i.getStock().equals(stock))
                .filter(i->i.getState() != 0)
                .filter(i -> i.getType().equals(MonitorFactory.WEEK_PRICE_INCREASE)
                        || i.getType().equals(MonitorFactory.WEEK_PRICE_DECREASE)
                        || i.getType().equals(MonitorFactory.PRICE_MONITOR)
                )
                .toList();

        if (list.isEmpty()) {
            return ResultGenerator.genSuccessResult(list);
        }

        MonthDetail currentMonthDetail = monthDataService.getCurrentMonthDetail(stock);

        List<MonitorPriceDTO> ret = new ArrayList<>();
        list.forEach(i -> {
            MonitorPriceDTO monitorPriceDTO = new MonitorPriceDTO();

            BeanUtils.copyProperties(i, monitorPriceDTO);
            monitorPriceDTO.setMonitorPrice(Double.parseDouble(i.getExtra()));

            // 是否触发
            monitorPriceDTO.setTrigger(monitorFactory.trigger(i));

            // 当前价格
            monitorPriceDTO.setCurrentPrice(currentMonthDetail.getClose());

            monitorPriceDTO.setDisPct(
                    NumberUtil.calPct(monitorPriceDTO.getCurrentPrice() , monitorPriceDTO.getMonitorPrice())
            );

            ret.add(monitorPriceDTO);
        });

        return ResultGenerator.genSuccessResult(ret);
    }

    @GetMapping("price")
    public Result price() {
        List<Monitor> list = monitorService.getAllMonitors().stream()
                .filter(i->i.getState() != 0)
                .filter(i -> i.getType().equals(MonitorFactory.PRICE_MONITOR))
                .toList();

        if (list.isEmpty()) {
            return ResultGenerator.genSuccessResult(list);
        }

        List<MonitorPriceDTO> ret = new ArrayList<>();
        list.forEach(i -> {
            MonitorPriceDTO monitorPriceDTO = new MonitorPriceDTO();

            BeanUtils.copyProperties(i, monitorPriceDTO);
            monitorPriceDTO.setMonitorPrice(Double.parseDouble(i.getExtra()));

            MonthDetail currentMonthDetail = monthDataService.getCurrentMonthDetail(i.getStock());

            // 当前价格
            monitorPriceDTO.setCurrentPrice(currentMonthDetail.getClose());

            monitorPriceDTO.setDisPct(
                    NumberUtil.calPct(monitorPriceDTO.getCurrentPrice() , monitorPriceDTO.getMonitorPrice())
            );

            ret.add(monitorPriceDTO);
        });

        return ResultGenerator.genSuccessResult(ret);
    }
}
